package com.xiaoyao.novel.recovery;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import sun.util.logging.resources.logging;

import com.xiaoyao.novel.pojo.Book;
import com.xiaoyao.novel.pojo.Chapter;

public class QqxxbdParser {

	// <a href="book/353.html">雪鹰领主</a>
	static String bookListRegex = "<a href='book/(\\d+).html'>(.+?)</a>.+?\\s{3}(.+?)</font>";
	static Pattern bookListPattern = Pattern.compile(bookListRegex);

	// /<a href="/chapter/248609.html">第二百五十章 敲定</a>
	static String bookRegex = "<a href='/chapter/(\\d+).html'>(.+?)</a>";
	static Pattern bookPattern = Pattern.compile(bookRegex);

	static String keywordRegex = "<meta name='keywords' content='(.+?)'/>";
	static Pattern keywordPattern = Pattern.compile(keywordRegex);
	
	static String descRegex = "<meta name='description' content='(.+?)'/>";
	static Pattern descPattern = Pattern.compile(descRegex);
	
	static String chapterRegex = "<div class='txt'>(.+?)本文永久分享地址";
	static Pattern chapterPattern = Pattern.compile(chapterRegex ,Pattern.MULTILINE|Pattern.DOTALL);

	static Logger logger = Logger.getLogger(QqxxbdCrawl.class);
	 

	public static List<Book> parseBookList(String input) {
		List<Book> books = new ArrayList<Book>();
		input = input.replaceFirst(".+?<ul", "");
		input = input.replaceFirst("</ul.+", "");
		input = input.replaceAll("\"", "'");
		input = input.replaceAll("&nbsp;", " ");
		// <a href="book/353.html">雪鹰领主</a>
		Matcher matcher = bookListPattern.matcher(input);
		while (matcher.find()) {
			Book book = new Book();
			book.bookId = Integer.parseInt(matcher.group(1));
			book.bookName = matcher.group(2);
			book.authorName = matcher.group(3);
			books.add(book);
		}
		return books;
	}

	public static List<Chapter> parseBook(String input, int bookId) {
		List<Chapter> books = new ArrayList<Chapter>();
		input = input.replaceAll("\"", "'");
		input = input.replaceFirst("DOC.+?<div class='category'>", "");
		input = input.replaceFirst("</div.+", " ");
//		logger.info(input) ;
		// <a href="book/353.html">雪鹰领主</a>
		Matcher matcher = bookPattern.matcher(input);
		while (matcher.find()) {
			Chapter chapter = new Chapter();
			chapter.bookId = bookId;
			chapter.cid = Integer.parseInt(matcher.group(1));
			chapter.chapterTitle = matcher.group(2);
			// chapter.chapterContent=matcher.group(2);
			// chapter.keywords ="" ;
			// chapter.description="" ;
			books.add(chapter);
		}

		return books;
	}

	public static Chapter parseChapter(String input, Chapter chapter) {
		input = input.replaceAll("\"", "'");
		System.out.println(input);
		// <a href="book/353.html">雪鹰领主</a>
		Matcher matcher1=keywordPattern.matcher(input);
		if (matcher1.find()) {
			chapter.keywords = matcher1.group(1); 
		}

		Matcher matcher2 = descPattern.matcher(input);
		if (matcher2.find()) { 
			chapter.description = matcher2.group(1); 
		}
		Matcher matcher3 = chapterPattern.matcher(input);
		if (matcher3.find()) { 
			chapter.chapterContent = matcher3.group(1);
		}
		return chapter;
	}

}
